﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DataLibrary;
using Common;
using System.Drawing.Printing;

namespace TicketSystem
{
    public partial class UC_Guide : UserControl
    {
        public UC_Guide()
        {
            InitializeComponent();
        }
        private TicketSystemEntities tse = new TicketSystemEntities();
        private PublicMethod pm = new PublicMethod();

        private void UC_Guide_Load(object sender, EventArgs e)
        {
            loaddata();
        }
       
        /// <summary>
        /// 导游列表
        /// </summary>
        public void loaddata()
        {
            var guide = tse.Guides.Where(u => true).Select(u => new { u.Name, u.Sex, u.Phone, u.GuideCard }).ToList();
            this.dataGridView1.DataSource = guide;
            this.dataGridView1.Columns[0].HeaderText = "姓名";
            this.dataGridView1.Columns[1].HeaderText = "性别";
            this.dataGridView1.Columns[2].HeaderText = "联系电话";
            this.dataGridView1.Columns[3].HeaderText = "导游卡号";
            int roleid = tse.Operators.Where(u => u.OperatorID == GetOperator.OperatorID).FirstOrDefault().RoleID;
            List<string> l = new List<string>();
            var right = tse.RoleRights.Where(u => u.RoieID == roleid);
            foreach (var r in right)
            {
                l.Add(r.Rights.RightName);
            }
            //验证操作员是否可以打印和导出导游信息
            pm.CanExport(this.btnExport, l);
            pm.CanPrint(this.btnPrint, l);
        }

        /// <summary>
        /// 添加导游
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {

            using (Form_GuideAdd a = new Form_GuideAdd())
            {
                a.ShowDialog();
                //这里调用绑定函数
                loaddata();
            }
        }
        /// <summary>
        /// 编辑导游
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdata_Click(object sender, EventArgs e)
        {
            int rows = this.dataGridView1.SelectedRows.Count;
            if (rows > 1)
            {
                MessageBox.Show("一次只能修改一行");
            }
            else if (rows < 1)
            {
                MessageBox.Show("你当前没有选中任何行");
            }
            else
            {
                string guidecard = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
                using (Form_GuideUpdate gu = new Form_GuideUpdate())
                {
                    gu.GuideCard = guidecard;
                    gu.ShowDialog();
                    loaddata();
                }
            }
        }
        /// <summary>
        /// 删除导游
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            var count = this.dataGridView1.SelectedRows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    string guidecard = this.dataGridView1.SelectedRows[i].Cells[3].Value.ToString();
                    var guide = tse.Guides.Where(u => u.GuideCard == guidecard);
                    tse.Guides.RemoveRange(guide);
                }
                int result = tse.SaveChanges();
                if (result > 0)
                {
                    loaddata();
                }
                else
                {
                    MessageBox.Show("删除失败了！请稍后重试");
                }
            }
            else
            {
                MessageBox.Show("你当前没有选中任何行！");
            }

        }
        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrint_Click(object sender, EventArgs e)
        {
            var printPreviewDialog = new PrintPreviewDialog { Document = printDocument1 };
            printPreviewDialog.ShowDialog();

        }
        private GridPrinter InitializePrinting()
        {
            var printDialog = new PrintDialog();
            if (printDialog.ShowDialog() != DialogResult.OK)
                return new GridPrinter();
            printDocument1.DocumentName = "导游基本信息";
            printDocument1.PrinterSettings = printDialog.PrinterSettings;
            printDocument1.DefaultPageSettings = printDialog.PrinterSettings.DefaultPageSettings;
            printDocument1.DefaultPageSettings.Margins = new Margins(40, 40, 40, 40);

            var gridPrinter = new GridPrinter(dataGridView1, printDocument1, true, true, "导游基本信息",
                new System.Drawing.Font("黑体", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Blue, true);
            return gridPrinter;
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, EventArgs e)
        {
            pm.export(this.dataGridView1);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            //this.Close();
        }

        private void printDocument1_PrintPage_1(object sender, PrintPageEventArgs e)
        {
            //var gp = InitializePrinting();
            //var datagrid = dataGridView1;
            //Font a = new Font("宋体", 25);
            //datagrid.Font = a;
            //try
            //{
            //    var more = gp.DrawDataGridView(e.Graphics, datagrid);
            //    if (more)
            //        e.HasMorePages = true;
            //    Font b = new Font("宋体", 9);
            //    this.dataGridView1.Font = b;
            //}
            //catch
            //{
            //    MessageBox.Show("打印异常");
            //}
            Common.PrintDataGridView.Print(dataGridView1, "导游列表", "", 0, 0);
        }

                        
    }
}
